package com.slz3.business.module.systemManagement.sysRolePermissions.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.module.systemManagement.sysRolePermissions.domain.SysRolePermissions;
import com.slz3.business.module.systemManagement.sysRolePermissions.domain.SysRolePermissionsFactory;
import com.slz3.business.module.systemManagement.sysRolePermissions.steward.SysRolePermissionsSpecification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.*;

@Slf4j
@Service
public class SysRolePermissionsServiceImpl implements SysRolePermissionsService {
    @Override
    public SysRolePermissions save(SysRolePermissions sysRolePermissions) {
        return Repositorys.sysRolePermissions.save(SysRolePermissionsFactory.initBaseFields(sysRolePermissions));
    }
    @Override
    public Page<SysRolePermissions> page(MultiValueMap<String, String> params) {
        Page<SysRolePermissions> sysRolePermissionsPage = Repositorys.sysRolePermissions.findAll(
                SysRolePermissionsSpecification.findJoin(params), PageableSteward.extract(params)
        );
        return sysRolePermissionsPage;
    }


    @Override
    public SysRolePermissions modify(SysRolePermissions sysRolePermissionsNew) {
        return Repositorys.sysRolePermissions.saveAndFlush(SysRolePermissionsFactory.initBaseFields(sysRolePermissionsNew));
    }

    @Override
    public Optional<SysRolePermissions> single(UUID uuid) {
        return Repositorys.sysRolePermissions.findById(uuid);
    }

    @Override
    public List<SysRolePermissions> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.sysRolePermissions.findAllById(uuids);
    }

    @Override
    public List<SysRolePermissions> delete(List<SysRolePermissions> sysRolePermissionsList) {
        sysRolePermissionsList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            SysRolePermissionsFactory.initBaseFields(item);
        });
        return Services.sysRolePermissionsTransactional.batchModify(sysRolePermissionsList);
    }

}
